package com.company.indicationshots.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.widget.ImageView;

import com.company.indicationshots.R;
import com.company.indicationshots.model.Score;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
//射靶分析页面靶面描点绘制
public class AnalyseTargetResultView {
    public void getTargetView(Context context, List<Map<String, Object>> shotList, List<Map<String, Object>> aimList, ImageView imageView) {

        // 加载背景图片
        Bitmap backgroundBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.chest_ring);
        // 创建一个新的Bitmap对象，尺寸与背景图片相同（或根据需要调整）
        Bitmap combinedBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), Bitmap.Config.ARGB_8888);

        // 初始化Canvas
        Canvas canvas = new Canvas(combinedBitmap);

        // 绘制背景图片
        canvas.drawBitmap(backgroundBitmap, 0, 0, null);

        //绘制射靶弹着点
        Bitmap sticker;
        Bitmap scaledStickerBitmap;

        Map<String, Object> map;
        int newWidth = 40; // 新的宽度
        int newHeight = 40; // 新的高度

        for (int i = 0; i < shotList.size(); i++) {
            map = shotList.get(i);
            // 加载贴图图片并绘制到Canvas上
            if (i == shotList.size() - 1) {
                sticker = BitmapFactory.decodeResource(context.getResources(), R.drawable.bullet_hole_selected);
            } else {
                sticker = BitmapFactory.decodeResource(context.getResources(), R.drawable.bullet_hole);
            }
            // 创建一个缩放后的Bitmap对象
            scaledStickerBitmap = Bitmap.createScaledBitmap(sticker, newWidth, newHeight, false);
            // 绘制贴图图片到Canvas上
            canvas.drawBitmap(scaledStickerBitmap, (float) map.get("x"), (float) map.get("y"), null);
        }

        //绘制瞄准点轨迹
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);

        Map<String, Object> map1;
        Map<String, Object> map2;

        for (int i = 0; i < aimList.size(); i++) {
            map2 = aimList.get(i);
            if (i == aimList.size() - 1) {
                paint.setStyle(Paint.Style.FILL);
                paint.setTextSize(120);
                paint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText("+", (float) map2.get("x") + 20, (float) map2.get("y") + 60, paint);
            }
            if (i > 0) {
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(5);
                map1 = aimList.get(i - 1);
                canvas.drawLine((float) map1.get("x") + 20, (float) map1.get("y") + 20, (float) map2.get("x") + 20, (float) map2.get("y") + 20, paint);
            }
        }

        // 将合并后的图片设置到ImageView上
        imageView.setImageBitmap(combinedBitmap);
    }
}
